package com.aiti.lulian.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ReUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.bo.CardClusterInfoBo;
import com.aiti.lulian.dto.CardClusterInfoDto;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.crad.CardZfSort;
import com.aiti.lulian.entity.crad.cluster.CardClusterEnterpriseBinding;
import com.aiti.lulian.entity.crad.cluster.CardClusterInfo;
import com.aiti.lulian.entity.excel.CardClusterEnterpriseImport;
import com.aiti.lulian.mapper.LuLianEnterpriseMapper;
import com.aiti.lulian.mapper.card.cluster.CardClusterInfoMapper;
import com.aiti.lulian.mapper.card.cluster.CardZfSortMapper;
import com.aiti.lulian.service.ICardClusterEnterpriseBindingService;
import com.aiti.lulian.service.ICardClusterEnterpriseImportService;
import com.aiti.lulian.service.ICardClusterInfoService;
import com.aiti.lulian.vo.CardClusterDetailInfo;
import com.aiti.lulian.vo.CardClusterExportInfo;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CardClusterInfoServiceImpl extends BaseSerivceImpl<CardClusterInfoMapper, CardClusterInfo> implements ICardClusterInfoService {

    private final CardClusterInfoMapper baseMapper;

    private final ICardClusterEnterpriseBindingService cardClusterEnterpriseBindingService;

    private final CardZfSortMapper cardZfSortMapper;

    private final LuLianEnterpriseMapper luLianEnterpriseMapper;

    private final ICardClusterEnterpriseImportService cardClusterEnterpriseImportService;

    public CardClusterInfoServiceImpl(CardClusterInfoMapper baseMapper, ICardClusterEnterpriseBindingService cardClusterEnterpriseBindingService, CardZfSortMapper cardZfSortMapper, LuLianEnterpriseMapper luLianEnterpriseMapper, ICardClusterEnterpriseImportService cardClusterEnterpriseImportService) {
        this.baseMapper = baseMapper;
        this.cardClusterEnterpriseBindingService = cardClusterEnterpriseBindingService;
        this.cardZfSortMapper = cardZfSortMapper;
        this.luLianEnterpriseMapper = luLianEnterpriseMapper;
        this.cardClusterEnterpriseImportService = cardClusterEnterpriseImportService;
    }

    /**
     * 新增产业集群
     *
     * @param bo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCardClusterInfo(CardClusterInfoBo bo,String userId,String enterpriseId) {
        CardClusterInfo params = new CardClusterInfo();

        params.setClusterDesc(bo.getClusterDesc());
        params.setCity(bo.getCity());
        params.setProvince(bo.getProvince());
        params.setCounty(bo.getCounty());
        params.setClusterName(bo.getClusterName());
        params.setBackgroundPic(bo.getBackgroundPic());
        params.setEnterpriseId(enterpriseId);

        params.setBaseCreateBy(userId);
        params.setBaseCreateTime(DateUtil.date());
        params.setBaseUpdateTime(DateUtil.date());
        params.setBaseUpdateBy(userId);

        //查询该用户下的所有集群
        QueryWrapper<CardClusterInfo> queryWrapper = new QueryWrapper<CardClusterInfo>();
        queryWrapper.eq("enterprise_id",enterpriseId);
        queryWrapper.select("cluster_name");
        List<CardClusterInfo> cardClusterInfos = baseMapper.selectList(queryWrapper);
        StringBuilder sb = new StringBuilder().append(bo.getClusterName());
        if(cardClusterInfos != null && !cardClusterInfos.isEmpty()){
            String collect = cardClusterInfos.stream().map(CardClusterInfo::getClusterName).collect(Collectors.joining(","));
            sb.append(",").append(collect);
        }
        int insert = baseMapper.insert(params);

        QueryWrapper<CardZfSort> deleqw = new QueryWrapper<>();
        deleqw.eq("enterprise_id",enterpriseId);
        deleqw.eq("type","jq");
        CardZfSort cardZfSort1 = cardZfSortMapper.selectOne(deleqw);
        if(cardZfSort1 != null){
            String id = cardZfSort1.getId();
            CardZfSort cardZfSort = new CardZfSort();
            cardZfSort.setContent(sb.toString());
            cardZfSort.setId(id);
            cardZfSort.setType("jq");
            cardZfSortMapper.updateById(cardZfSort);
        }else{
            CardZfSort cardZfSort = new CardZfSort();
            cardZfSort.setId(IdUtil.simpleUUID());
            cardZfSort.setContent(sb.toString());
            cardZfSort.setBaseCreateBy(userId);
            cardZfSort.setType("jq");
            cardZfSort.setBaseCreateTime(DateUtil.date());
            cardZfSort.setEnterpriseId(enterpriseId);
            cardZfSortMapper.insert(cardZfSort);
        }

        List<CardClusterEnterpriseBinding> enterpriseInfo = bo.getEnterpriseInfo();
        if(enterpriseInfo != null && !enterpriseInfo.isEmpty()){
            List<CardClusterEnterpriseBinding> db = new ArrayList<>();
            for(CardClusterEnterpriseBinding item : enterpriseInfo){
                CardClusterEnterpriseBinding temp = new CardClusterEnterpriseBinding();
                boolean match = ReUtil.isMatch(".*[\\u4e00-\\u9fa5\\uf900-\\ufa2d].*", item.getEnterpriseId());
                if(match){
                    //如果ID中包含中文，则判断为手动输入
                    temp.setEnterpriseId(null);
                    temp.setEnterpriseName(item.getEnterpriseId());
                }else{
                    temp.setEnterpriseId(item.getEnterpriseId());
                }
                temp.setClusterId(params.getBaseId());
                temp.setBaseId(IdUtil.simpleUUID());
                temp.setBaseCreateTime(DateUtil.date());
                temp.setBaseCreateBy(userId);
                db.add(temp);
            }
            cardClusterEnterpriseBindingService.saveBatch(db);
        }
        return insert > 0;
    }


    /**
     * 查询产业集群详情
     *
     * @param userId 创建人Id
     * @param bo     参数
     * @return 结果
     */
    @Override
    public CardClusterDetailInfo queryClusterDetailInfo(String userId, CardClusterInfoBo bo) {
        bo.setBaseCreateBy(userId);
//        LambdaQueryWrapper<CardClusterInfo> lambdaQw = buildLambdaQw(bo);
//        CardClusterInfo cardClusterInfo = baseMapper.selectOne(lambdaQw);

//        CardClusterDetailInfo result = new CardClusterDetailInfo();
//        if(cardClusterInfo != null){
//            BeanUtil.copyProperties(cardClusterInfo,result,true);
//            List<CardClusterEnterpriseBinding> cardClusterEnterpriseBindings = baseMapper.selectClusterEnterpriseInfo(result.getBaseId());
//            result.setEnterpriseInfo(cardClusterEnterpriseBindings);
//        }
        CardClusterDetailInfo result = baseMapper.selectCardClusterDetailInfo(bo);
        return result;
    }

    @Override
    public List<CardClusterInfo> queryClusterList(Page<CardClusterInfo> page, CardClusterInfoBo bo) {
        List<CardClusterInfo> cardClusterInfoPage = baseMapper.selectCustomePage(page,bo);

        return cardClusterInfoPage;
    }

    private LambdaQueryWrapper<CardClusterInfo> buildLambdaQw(CardClusterInfoBo bo) {
        LambdaQueryWrapper<CardClusterInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(bo.getBaseCreateBy()), CardClusterInfo::getBaseCreateBy, bo.getBaseCreateBy());
        queryWrapper.eq(StringUtils.isNotBlank(bo.getBaseId()), CardClusterInfo::getBaseId, bo.getBaseId());
        return queryWrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCardClusterInfo(CardClusterInfoBo bo) {
        CardClusterInfo params = new CardClusterInfo();

        params.setBaseId(bo.getBaseId());
        params.setClusterName(bo.getClusterName());
        params.setClusterDesc(bo.getClusterDesc());
        params.setProvince(bo.getProvince());
        params.setCity(bo.getCity());
        params.setCounty(bo.getCounty());
        params.setBackgroundPic(bo.getBackgroundPic());
        params.setBaseUpdateBy(bo.getBaseUpdateBy());
        params.setBaseUpdateTime(DateUtil.date());

        baseMapper.updateById(params);

        //查询该用户下的所有集群
        QueryWrapper<CardClusterInfo> queryWrapper = new QueryWrapper<CardClusterInfo>();
        queryWrapper.eq("base_create_by",bo.getBaseCreateBy());
        queryWrapper.select("cluster_name");
        List<CardClusterInfo> cardClusterInfos = baseMapper.selectList(queryWrapper);
        StringBuilder sb = new StringBuilder();
        if(cardClusterInfos != null && !cardClusterInfos.isEmpty()){
            String collect = cardClusterInfos.stream().map(CardClusterInfo::getClusterName).collect(Collectors.joining(","));
            sb.append(collect);
        }

        QueryWrapper<CardZfSort> deleqw=  new QueryWrapper<>();
        deleqw.eq("base_create_by",bo.getBaseCreateBy());
        deleqw.eq("type","jq");
        CardZfSort cardZfSort1 = cardZfSortMapper.selectOne(deleqw);
        if(cardZfSort1 != null){
            String id = cardZfSort1.getId();
            CardZfSort cardZfSort = new CardZfSort();
            cardZfSort.setContent(sb.toString());
            cardZfSort.setId(id);
            cardZfSortMapper.updateById(cardZfSort);
        }else{
            CardZfSort cardZfSort = new CardZfSort();
            cardZfSort.setId(IdUtil.simpleUUID());
            cardZfSort.setContent(sb.toString());
            cardZfSortMapper.insert(cardZfSort);
        }

        List<CardClusterEnterpriseBinding> enterpriseInfo = bo.getEnterpriseInfo();
        LambdaQueryWrapper<CardClusterEnterpriseBinding> lambdaQw = new LambdaQueryWrapper<>();
        lambdaQw.eq(CardClusterEnterpriseBinding::getClusterId,bo.getBaseId());
        cardClusterEnterpriseBindingService.remove(lambdaQw);

        if(enterpriseInfo != null && !enterpriseInfo.isEmpty()){
            List<CardClusterEnterpriseBinding> db = new ArrayList<>();
            for(CardClusterEnterpriseBinding item : enterpriseInfo){
                CardClusterEnterpriseBinding temp = new CardClusterEnterpriseBinding();
                boolean match = ReUtil.isMatch(".*[\\u4e00-\\u9fa5\\uf900-\\ufa2d].*", item.getEnterpriseId());
                if(match){
                    //如果ID中包含中文，则判断为手动输入
                    temp.setEnterpriseId(null);
                    temp.setEnterpriseName(item.getEnterpriseId());
                }else{
                    temp.setEnterpriseId(item.getEnterpriseId());
                }
                temp.setClusterId(params.getBaseId());
                temp.setBaseId(IdUtil.simpleUUID());
                temp.setBaseCreateTime(DateUtil.date());
                temp.setBaseCreateBy(bo.getBaseUpdateBy());
                db.add(temp);
            }
            cardClusterEnterpriseBindingService.saveBatch(db);
        }
    }

    @Override
    public List<CardClusterEnterpriseBinding> selectEnterpriseInfoByClusterId(String clusterId) {
        return baseMapper.selectClusterEnterpriseInfo(clusterId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message deleteClusterById(String baseId,String userId) {
        LambdaQueryWrapper<CardClusterEnterpriseBinding> lambdaQw = new LambdaQueryWrapper<>();
        lambdaQw.eq(CardClusterEnterpriseBinding::getClusterId,baseId);
        baseMapper.deleteById(baseId);
        cardClusterEnterpriseBindingService.remove(lambdaQw);


        QueryWrapper<CardClusterInfo> queryWrapper = new QueryWrapper<CardClusterInfo>();
        queryWrapper.eq("base_create_by",userId);
        queryWrapper.select("cluster_name");
        List<CardClusterInfo> cardClusterInfos = baseMapper.selectList(queryWrapper);
        StringBuilder sb = new StringBuilder();
        if(cardClusterInfos != null && !cardClusterInfos.isEmpty()){
            String collect = cardClusterInfos.stream().map(CardClusterInfo::getClusterName).collect(Collectors.joining(","));
            sb.append(collect);
        }

        QueryWrapper<CardZfSort> deleqw=  new QueryWrapper<>();
        deleqw.eq("base_create_by",userId);
        deleqw.eq("type","jq");
        CardZfSort cardZfSort1 = cardZfSortMapper.selectOne(deleqw);
        if(cardZfSort1 != null){
            String id = cardZfSort1.getId();
            CardZfSort cardZfSort = new CardZfSort();
            cardZfSort.setContent(sb.toString());
            cardZfSort.setId(id);
            cardZfSortMapper.updateById(cardZfSort);
        }else{
            CardZfSort cardZfSort = new CardZfSort();
            cardZfSort.setId(IdUtil.simpleUUID());
            cardZfSort.setContent(sb.toString());
            cardZfSortMapper.insert(cardZfSort);
        }
        return Message.success();
    }

    @Override
    public List<CardClusterDetailInfo> getList(String baseId) {
        return baseMapper.selectClusterDetailInfoList(baseId);
    }

    @Override
    public List<CardClusterInfo> weiHu() {
        return baseMapper.weiHu();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message importClusterEnterpriseBatch(MultipartFile file, String userId) throws IOException {
        //构建报错信息集
        List<String> msgList = new ArrayList<>();
        List<CardClusterEnterpriseImport> importList = EasyExcel.read(file.getInputStream()).head(CardClusterEnterpriseImport.class).headRowNumber(1).sheet("集群企业").doReadSync();
        //构建要剔除的元素索引集
        List<CardClusterEnterpriseImport> removeList = new ArrayList<>();
        List<CardClusterEnterpriseImport> dataList = importList.stream().peek(i -> {
            LuLianEnterprise luLianEnterprise = luLianEnterpriseMapper.selectOne(new LambdaQueryWrapper<LuLianEnterprise>()
                    .eq(LuLianEnterprise::getUnicode, i.getUnicode())
                    .eq(LuLianEnterprise::getIsDelete, "0"));
            if (luLianEnterprise == null) {
                msgList.add("【" + i.getUnicode() + "】");
                removeList.add(i);
            } else {
                i.setEnterpriseName(luLianEnterprise.getBaseName());
                i.setEnterpriseId(luLianEnterprise.getBaseId());
                i.setCreator(userId);
                i.setBaseCreateTime(new Date());
            }
        }).collect(Collectors.toList());
        int size = removeList.size();
        if (size > 0) {
            for (int i = 0; i < size; i ++) {
                dataList.removeAll(removeList);
            }
        }
        //将处理好的导入数据存储到暂存表
        boolean saveBatch = cardClusterEnterpriseImportService.saveBatch(dataList);
        int msgListSize = msgList.size();
        if (saveBatch && msgListSize == 0) {
            return Message.success("导入成功");
        } else if (saveBatch && msgListSize > 0) {
            return Message.fail("统一信用代码为" + msgList.stream().collect(Collectors.joining("")) + "的企业未在系统中查询到，请重新编辑");
        } else {
            return Message.fail("导入失败");
        }
    }

    @Override
    public List<CardClusterExportInfo> clusterList(CardClusterInfoDto dto) {
        return this.baseMapper.clusterList(dto);
    }

    @Override
    public List<CardClusterDetailInfo> clusterPage(Page page, CardClusterInfoDto dto) {
        return this.baseMapper.clusterPage(page, dto);
    }

    @Override
    public List<CardClusterEnterpriseBinding> clusterEnterpriseList(String clusterId) {
        return this.baseMapper.clusterEnterpriseList(clusterId);
    }

    @Override
    public CardClusterDetailInfo clusterDetailInfo(String baseId) {
        CardClusterDetailInfo cardClusterDetailInfo = this.baseMapper.clusterDetailInfo(baseId);
        if(cardClusterDetailInfo != null){
            List<CardClusterEnterpriseBinding> cardClusterEnterpriseBindings = this.baseMapper.clusterEnterpriseList(baseId);
            cardClusterDetailInfo.setEnterpriseInfo(cardClusterEnterpriseBindings);

        }
        return cardClusterDetailInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCardClusterInfoNew(CardClusterInfoBo bo,String userId) {
        CardClusterInfo params = new CardClusterInfo();

        params.setClusterDesc(bo.getClusterDesc());
        params.setCity(bo.getCity());
        params.setProvince(bo.getProvince());
        params.setCounty(bo.getCounty());
        params.setClusterName(bo.getClusterName());
        params.setBackgroundPic(bo.getBackgroundPic());
        params.setEnterpriseId(bo.getEnterpriseId());
        params.setOtherContents(bo.getOtherContents());
        params.setBaseCreateBy(userId);
        params.setBaseCreateTime(DateUtil.date());
        params.setBaseUpdateTime(DateUtil.date());
        params.setBaseUpdateBy(userId);
        params.setLogoPic(bo.getLogoPic());

        //查询该用户下的所有集群
        QueryWrapper<CardClusterInfo> queryWrapper = new QueryWrapper<CardClusterInfo>();
        queryWrapper.eq("enterprise_id",bo.getEnterpriseId());
        queryWrapper.select("cluster_name");
//        List<CardClusterInfo> cardClusterInfos = baseMapper.selectList(queryWrapper);
//        StringBuilder sb = new StringBuilder().append(bo.getClusterName());
//        if(cardClusterInfos != null && !cardClusterInfos.isEmpty()){
//            String collect = cardClusterInfos.stream().map(CardClusterInfo::getClusterName).collect(Collectors.joining(","));
//            sb.append(",").append(collect);
//        }
        int insert = baseMapper.insert(params);

//        QueryWrapper<CardZfSort> deleqw = new QueryWrapper<>();
//        deleqw.eq("enterprise_id",enterpriseId);
//        deleqw.eq("type","jq");
//        CardZfSort cardZfSort1 = cardZfSortMapper.selectOne(deleqw);
//        if(cardZfSort1 != null){
//            String id = cardZfSort1.getId();
//            CardZfSort cardZfSort = new CardZfSort();
//            cardZfSort.setContent(sb.toString());
//            cardZfSort.setId(id);
//            cardZfSort.setType("jq");
//            cardZfSortMapper.updateById(cardZfSort);
//        }else{
//            CardZfSort cardZfSort = new CardZfSort();
//            cardZfSort.setId(IdUtil.simpleUUID());
//            cardZfSort.setContent(sb.toString());
//            cardZfSort.setBaseCreateBy(userId);
//            cardZfSort.setType("jq");
//            cardZfSort.setBaseCreateTime(DateUtil.date());
//            cardZfSort.setEnterpriseId(enterpriseId);
//            cardZfSortMapper.insert(cardZfSort);
//        }

        List<CardClusterEnterpriseBinding> enterpriseInfo = bo.getEnterpriseInfo();
        if(enterpriseInfo != null && !enterpriseInfo.isEmpty()){
            List<CardClusterEnterpriseBinding> db = new ArrayList<>();
            for(CardClusterEnterpriseBinding item : enterpriseInfo){
                CardClusterEnterpriseBinding temp = new CardClusterEnterpriseBinding();
                boolean match = ReUtil.isMatch(".*[\\u4e00-\\u9fa5\\uf900-\\ufa2d].*", item.getEnterpriseId());
                if(match){
                    //如果ID中包含中文，则判断为手动输入
                    temp.setEnterpriseId(null);
                    temp.setEnterpriseName(item.getEnterpriseId());
                }else{
                    temp.setEnterpriseId(item.getEnterpriseId());
                }
                temp.setClusterId(params.getBaseId());
                temp.setBaseId(IdUtil.simpleUUID());
                temp.setBaseCreateTime(DateUtil.date());
                temp.setBaseCreateBy(userId);
                temp.setEnterpriseName(item.getEnterpriseName());
                db.add(temp);
            }
            cardClusterEnterpriseBindingService.saveBatch(db);
        }
        return insert > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCardClusterInfoNew(CardClusterInfoBo bo) {
        CardClusterInfo params = new CardClusterInfo();

        params.setBaseId(bo.getBaseId());
        params.setClusterName(bo.getClusterName());
        params.setClusterDesc(bo.getClusterDesc());
        params.setProvince(bo.getProvince());
        params.setCity(bo.getCity());
        params.setCounty(bo.getCounty());
        params.setBackgroundPic(bo.getBackgroundPic());
        params.setBaseUpdateBy(bo.getBaseUpdateBy());
        params.setBaseUpdateTime(DateUtil.date());
        params.setOtherContents(bo.getOtherContents());
        params.setEnterpriseId(bo.getEnterpriseId());
        params.setLogoPic(bo.getLogoPic());

        baseMapper.updateById(params);

        //查询该用户下的所有集群
//        QueryWrapper<CardClusterInfo> queryWrapper = new QueryWrapper<CardClusterInfo>();
//        queryWrapper.eq("base_create_by",bo.getBaseCreateBy());
//        queryWrapper.select("cluster_name");
//        List<CardClusterInfo> cardClusterInfos = baseMapper.selectList(queryWrapper);
//        StringBuilder sb = new StringBuilder();
//        if(cardClusterInfos != null && !cardClusterInfos.isEmpty()){
//            String collect = cardClusterInfos.stream().map(CardClusterInfo::getClusterName).collect(Collectors.joining(","));
//            sb.append(collect);
//        }

//        QueryWrapper<CardZfSort> deleqw=  new QueryWrapper<>();
//        deleqw.eq("base_create_by",bo.getBaseCreateBy());
//        deleqw.eq("type","jq");
//        CardZfSort cardZfSort1 = cardZfSortMapper.selectOne(deleqw);
//        if(cardZfSort1 != null){
//            String id = cardZfSort1.getId();
//            CardZfSort cardZfSort = new CardZfSort();
//            cardZfSort.setContent(sb.toString());
//            cardZfSort.setId(id);
//            cardZfSortMapper.updateById(cardZfSort);
//        }else{
//            CardZfSort cardZfSort = new CardZfSort();
//            cardZfSort.setId(IdUtil.simpleUUID());
//            cardZfSort.setContent(sb.toString());
//            cardZfSortMapper.insert(cardZfSort);
//        }

        List<CardClusterEnterpriseBinding> enterpriseInfo = bo.getEnterpriseInfo();
        LambdaQueryWrapper<CardClusterEnterpriseBinding> lambdaQw = new LambdaQueryWrapper<>();
        lambdaQw.eq(CardClusterEnterpriseBinding::getClusterId,bo.getBaseId());
        cardClusterEnterpriseBindingService.remove(lambdaQw);

        if(enterpriseInfo != null && !enterpriseInfo.isEmpty()){
            List<CardClusterEnterpriseBinding> db = new ArrayList<>();
            for(CardClusterEnterpriseBinding item : enterpriseInfo){
                CardClusterEnterpriseBinding temp = new CardClusterEnterpriseBinding();
                boolean match = ReUtil.isMatch(".*[\\u4e00-\\u9fa5\\uf900-\\ufa2d].*", item.getEnterpriseId());
                if(match){
                    //如果ID中包含中文，则判断为手动输入
                    temp.setEnterpriseId(null);
                    temp.setEnterpriseName(item.getEnterpriseId());
                }else{
                    temp.setEnterpriseId(item.getEnterpriseId());
                }
                temp.setClusterId(params.getBaseId());
                temp.setBaseId(IdUtil.simpleUUID());
                temp.setBaseCreateTime(DateUtil.date());
                temp.setBaseCreateBy(bo.getBaseUpdateBy());
                temp.setEnterpriseName(item.getEnterpriseName());
                db.add(temp);
            }
            cardClusterEnterpriseBindingService.saveBatch(db);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message deleteClusterNew(String baseId) {
        LambdaQueryWrapper<CardClusterEnterpriseBinding> lambdaQw = new LambdaQueryWrapper<>();
        lambdaQw.eq(CardClusterEnterpriseBinding::getClusterId,baseId);
        baseMapper.deleteById(baseId);
        cardClusterEnterpriseBindingService.remove(lambdaQw);


//        QueryWrapper<CardClusterInfo> queryWrapper = new QueryWrapper<CardClusterInfo>();
//        queryWrapper.eq("base_create_by",userId);
//        queryWrapper.select("cluster_name");
//        List<CardClusterInfo> cardClusterInfos = baseMapper.selectList(queryWrapper);
//        StringBuilder sb = new StringBuilder();
//        if(cardClusterInfos != null && !cardClusterInfos.isEmpty()){
//            String collect = cardClusterInfos.stream().map(CardClusterInfo::getClusterName).collect(Collectors.joining(","));
//            sb.append(collect);
//        }

//        QueryWrapper<CardZfSort> deleqw=  new QueryWrapper<>();
//        deleqw.eq("base_create_by",userId);
//        deleqw.eq("type","jq");
//        CardZfSort cardZfSort1 = cardZfSortMapper.selectOne(deleqw);
//        if(cardZfSort1 != null){
//            String id = cardZfSort1.getId();
//            CardZfSort cardZfSort = new CardZfSort();
//            cardZfSort.setContent(sb.toString());
//            cardZfSort.setId(id);
//            cardZfSortMapper.updateById(cardZfSort);
//        }else{
//            CardZfSort cardZfSort = new CardZfSort();
//            cardZfSort.setId(IdUtil.simpleUUID());
//            cardZfSort.setContent(sb.toString());
//            cardZfSortMapper.insert(cardZfSort);
//        }
        return Message.success();
    }
}